/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "FCTableLayoutDiv.h"

namespace FaceCat{
    FCColumnStyle::FCColumnStyle()
    {
        m_sizeType = FCSizeType_PercentSize;
        m_width = 0;
    }
    
    FCColumnStyle::FCColumnStyle(FCSizeType sizeType, float width){
        m_sizeType = sizeType;
        m_width = width;
    }
    
    FCColumnStyle::~FCColumnStyle(){
    }
    
    FCSizeType FCColumnStyle::getSizeType(){
        return m_sizeType;
    }
    
    void FCColumnStyle::setSizeTypeA(FCSizeType  value){
        m_sizeType = value;
    }
    
    float FCColumnStyle::getWidth(){
        return m_width;
    }
    
    void FCColumnStyle::setWidth(float value){
        m_width = value;
    }
    
    void FCColumnStyle::getAttribute(const String& name, String *value, String *type){
        if (name == L"sizetype"){
            *type = L"enum:FCSizeType";
            if (m_sizeType == FCSizeType_AbsoluteSize){
                *value = L"absolutesize";
            }
            else if (m_sizeType == FCSizeType_AutoFill){
                *value = L"autofill";
            }
            else if (m_sizeType == FCSizeType_PercentSize){
                *value = L"percentsize";
            }
        }
        else if (name == L"width"){
            *type = L"float";
            *value = FCTran::floatToStr(getWidth());
        }
    }
    
    ArrayList<String> FCColumnStyle::getAttributeNames(){
        ArrayList<String> attributeNames;
        attributeNames.add(L"SizeType");
        attributeNames.add(L"Height");
        return attributeNames;
    }
    
    void FCColumnStyle::setAttribute(const String& name, const String& value){
        if (name == L"sizetype"){
            String lowerStr = FCTran::toLower(value);
            if (value == L"absolutesize"){
                m_sizeType = FCSizeType_AbsoluteSize;
            }
            else if (value == L"autofill"){
                m_sizeType = FCSizeType_AutoFill;
            }
            else if (value == L"percentsize"){
                m_sizeType = FCSizeType_PercentSize;
            }
        }
        else if (name == L"width"){
            setWidth(FCTran::strToFloat(value));
        }
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    FCRowStyle::FCRowStyle(){
        m_sizeType = FCSizeType_PercentSize;
        m_height = 0;
    }
    
    FCRowStyle::FCRowStyle(FCSizeType sizeType, float height){
        m_height = height;
        m_sizeType = sizeType;
    }
    
    FCRowStyle::~FCRowStyle(){
    }
    
    float FCRowStyle::getHeight(){
        return m_height;
    }
    
    void FCRowStyle::setHeight(float height){
        m_height = height;
    }
    
    FCSizeType FCRowStyle::getSizeType(){
        return m_sizeType;
    }
    
    void FCRowStyle::setSizeTypeA(FCSizeType  sizeType){
        m_sizeType = sizeType;
    }
    
    void FCRowStyle::getAttribute(const String& name, String *value, String *type){
        if (name == L"sizetype"){
            *type = L"enum:FCSizeType";
            if (m_sizeType == FCSizeType_AbsoluteSize){
                *value = L"absolutesize";
            }
            else if (m_sizeType == FCSizeType_AutoFill){
                *value = L"autofill";
            }
            else if (m_sizeType == FCSizeType_PercentSize){
                *value = L"percentsize";
            }
        }
        else if (name == L"height"){
            *type = L"float";
            *value = FCTran::floatToStr(getHeight());
        }
    }
    
    ArrayList<String> FCRowStyle::getAttributeNames(){
        ArrayList<String> attributeNames;
        attributeNames.add(L"SizeType");
        attributeNames.add(L"Height");
        return attributeNames;
    }
    
    void FCRowStyle::setAttribute(const String& name, const String& value){
        if (name == L"sizetype"){
            String lowerStr = FCTran::toLower(value);
            if (value == L"absolutesize"){
                m_sizeType = FCSizeType_AbsoluteSize;
            }
            else if (value == L"autofill"){
                m_sizeType = FCSizeType_AutoFill;
            }
            else if (value == L"percentsize"){
                m_sizeType = FCSizeType_PercentSize;
            }
        }
        else if (name == L"height"){
            setHeight(FCTran::strToFloat(value));
        }
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    FCTableLayoutDiv::FCTableLayoutDiv(){
        m_columnsCount = 0;
        m_rowsCount = 0;
    }
    
    FCTableLayoutDiv::~FCTableLayoutDiv(){
        m_columns.clear();
        m_columnStyles.clear();
        m_rows.clear();
        m_rowStyles.clear();
        m_tableViews.clear();
    }
    
    int FCTableLayoutDiv::getColumnsCount(){
        return m_columnsCount;
    }
    
    void FCTableLayoutDiv::setColumnsCount(int columnsCount){
        m_columnsCount = columnsCount;
    }
    
    int FCTableLayoutDiv::getRowsCount(){
        return m_rowsCount;
    }
    
    void FCTableLayoutDiv::setRowsCount(int rowsCount){
        m_rowsCount = rowsCount;
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    void FCTableLayoutDiv::addView(FCView *view){
        ArrayList<FCView*> views = getViews();
        int viewsSize = (int)views.size();
        FCDiv::addView(view);
        int column = viewsSize % m_columnsCount;
        int row = viewsSize / m_columnsCount;
        m_columns.add(column);
        m_rows.add(row);
        m_tableViews.add(view);
    }
    
    void FCTableLayoutDiv::addView(FCView *view, int column, int row){
        FCDiv::addView(view);
        m_columns.add(column);
        m_rows.add(row);
        m_tableViews.add(view);
    }
    
    String FCTableLayoutDiv::getViewType(){
        return L"TableLayoutDiv";
    }
    
    void FCTableLayoutDiv::getAttribute(const String& name, String *value, String *type){
        if (name == L"columnscount"){
            *type = L"int";
            *value = FCTran::intToStr(getColumnsCount());
        }
        else if (name == L"rowscount"){
            *type = L"int";
            *value = FCTran::intToStr(getRowsCount());
        }
        else{
            FCDiv::getAttribute(name, value, type);
        }
    }
    
    ArrayList<String> FCTableLayoutDiv::getAttributeNames(){
        ArrayList<String> attributeNames = FCDiv::getAttributeNames();
        attributeNames.add(L"ColumnsCount");
        attributeNames.add(L"RowsCount");
        return attributeNames;
    }
    
    bool FCTableLayoutDiv::onResetLayout(){
        if (getNative()){
            if(m_columnsCount > 0 && m_rowsCount > 0 && (int)m_columnStyles.size() > 0 && (int)m_rowStyles.size() > 0){
                int width = getWidth(), height = getHeight();
                int tableViewsSize = (int)m_tableViews.size();
                int *columnWidths = new int[m_columnsCount];
                int *rowHeights = new int[m_rowsCount];
                int allWidth = 0, allHeight = 0;
                for (int i = 0; i < m_columnsCount; i++){
                    FCColumnStyle columnStyle = m_columnStyles.get(i);
                    int cWidth = 0;
                    FCSizeType sizeType = columnStyle.getSizeType();
                    float sWidth = columnStyle.getWidth();
                    if (sizeType == FCSizeType_AbsoluteSize){
                        cWidth = (int)(sWidth);
                    }
                    else if (sizeType == FCSizeType_AutoFill){
                        cWidth = width - allWidth;
                    }
                    else if (sizeType == FCSizeType_PercentSize){
                        cWidth = (int)(width * sWidth);
                    }
                    columnWidths[i] = cWidth;
                    allWidth += cWidth;
                }
                for (int i = 0; i < m_rowsCount; i++){
                    FCRowStyle rowStyle = m_rowStyles.get(i);
                    int rHeight = 0;
                    FCSizeType sizeType = rowStyle.getSizeType();
                    float sHeight = rowStyle.getHeight();
                    if (sizeType == FCSizeType_AbsoluteSize){
                        rHeight = (int)(sHeight);
                    }
                    else if (sizeType == FCSizeType_AutoFill){
                        rHeight = height - allHeight;
                    }
                    else if (sizeType == FCSizeType_PercentSize){
                        rHeight = (int)(height * sHeight);
                    }
                    rowHeights[i] = rHeight;
                    allHeight += rHeight;
                }
                for (int i = 0; i < tableViewsSize; i++){
                    FCView *view = m_tableViews.get(i);
                    int column = m_columns.get(i);
                    int row = m_rows.get(i);
                    FCPadding margin = view->getMargin();
                    int cLeft = 0, cTop = 0;
                    for (int j = 0; j < column; j++){
                        cLeft += columnWidths[j];
                    }
                    for (int j = 0; j < row; j++){
                        cTop += rowHeights[j];
                    }
                    int cRight = cLeft + columnWidths[column] - margin.right;
                    int cBottom = cTop + rowHeights[row] - margin.bottom;
                    cLeft += margin.left;
                    cTop += margin.top;
                    if (cRight < cLeft){
                        cRight = cLeft;
                    }
                    if (cBottom < cTop){
                        cBottom = cTop;
                    }
                    FCRect bounds ={cLeft, cTop, cRight, cBottom};
                    view->setBounds(bounds);
                }
            }
        }
        return true;
    }
    
    void FCTableLayoutDiv::removeView(FCView *view){
        int tableViewsSize = m_tableViews.size();
        int index = -1;
        for (int i = 0; i < tableViewsSize; i++){
            if (view == m_tableViews.get(i)){
                index = i;
                break;
            }
        }
        if (index != -1){
            m_columns.removeAt(index);
            m_rows.removeAt(index);
            m_tableViews.removeAt(index);
        }
        FCDiv::removeView(view);
    }
    
    void FCTableLayoutDiv::setAttribute(const String& name, const String& value){
        if (name == L"columnscount"){
            setColumnsCount(FCTran::strToInt(value));
        }
        else if (name == L"rowscount"){
            setRowsCount(FCTran::strToInt(value));
        }
        else{
            FCDiv::setAttribute(name, value);
        }
    }
    
    void FCTableLayoutDiv::update(){
        if(m_native && !m_native->m_lockUpdate){
            onResetLayout();
            int viewsSize = (int)m_views.size();
            for (int i = 0; i < viewsSize; i++){
                m_views.get(i)->update();
            }
            updateScrollBar();
        }
    }
}
